﻿#include "dingdanpickerstandarditemmodel.h"

DingdanPickerStandardItemModel::DingdanPickerStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"日期"
         <<"单号"
        <<"类型"
       <<"状态"
      <<"客户名称"
     <<"联系人"
    <<"电话"
    <<"数量合计"
    <<"金额合计"
    <<"操作员"
    <<"备注";
    setHorizontalHeaderLabels(header);
}

void DingdanPickerStandardItemModel::reset(){
    clear();
    dingdans.clear();
    setHorizontalHeaderLabels(header);
}

void DingdanPickerStandardItemModel::insertDingdan(QList<Dingdan*> dingdans, int type){
    for(Dingdan *dingdan:dingdans){
        insertDingdan(*dingdan, type);
    }
}

void DingdanPickerStandardItemModel::setWidgetType(Dingdan::DingdanType widgetType){
    if(widgetType == Dingdan::SaleOut || widgetType == Dingdan::SaleBack){
        header[4] = "客户名称";
    }else{
        header[4] = "供应商名称";
    }
}

void DingdanPickerStandardItemModel::insertDingdan(Dingdan &dingdan, int type){
    QList<QStandardItem*> rowData;
    UserRelation *relation;
    if(type == 0){
        //我方单据
        relation = dingdan.getUserRelation();
    }else{
        //对方单据
        relation = dingdan.getReverseRelation();
    }

    rowData.append(new QStandardItem(dingdan.date));
    rowData.append(new QStandardItem(dingdan.dingdanId));
    rowData.append(new QStandardItem(dingdan.getTypeStr()));
    rowData.append(new QStandardItem(dingdan.getStateStr()));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->nickName));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->contact));
    rowData.append(new QStandardItem(relation==nullptr?"":relation->tel));
    rowData.append(new QStandardItem(QString("%1").arg(dingdan.totalCount)));
    rowData.append(new QStandardItem(QString::number(dingdan.totalMoney, 'f', 2)));
    rowData.append(new QStandardItem(dingdan.opNickName));
    rowData.append(new QStandardItem(dingdan.remark));

    dingdans.append(&dingdan);
    appendRow(rowData);
}

Dingdan* DingdanPickerStandardItemModel::getDingdan(int index){
    return dingdans[index];
}
